/*
 * Adara WebCam TimeLapse capture software.
 * Copyright (C) 2012 AdaraProjectTeam
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package adara.plugins;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;

import adara.commons.ResourceDiscovery;

/**
 * 
 * @author Andrei Ermicioi (aka erani) <andrei.ermicioi@gmail.com>
 *
 */
public class PluginManager {
	
	private static final Logger logger = Logger.getLogger(PluginManager.class);

	private static PluginManager instance = null;
	
	private Configuration pluginsConfiguration;
	private List<Class<Plugin>> listOfPlugins;
	
	private PluginManager() {
		this.load();
	}
	
	public static Collection<Plugin> getPlugins() {
		return instance().factoryPluginsSet();
	}
	
	public static Configuration getProperty() {
		return instance().pluginsConfiguration;
	}
	
	public static PluginManager instance() {
		if (instance == null) {
			synchronized (PluginManager.class) {
				if (instance == null) {
					instance = new PluginManager();
				}
			}
		}
		
		return instance;
	}
	
	private void load() {
		listOfPlugins = new ArrayList<Class<Plugin>>();
		
		File cfgfile = new File(
			ResourceDiscovery.getClasspathResource("plugins.properties"));
		
		if (logger.isDebugEnabled()) {
			logger.debug("Plugins configuration file located at " 
					+ cfgfile.getAbsolutePath());
		}
		
		if (!cfgfile.exists()) {
			logger.error("Plugins configuration file is not found");
			return;
		}
		
		try {
			pluginsConfiguration = new PropertiesConfiguration(cfgfile);
		} catch (ConfigurationException e) {
			logger.error("Could not load plugins. Plugins engine off", e);
			return;
		}
		
		for (String pluginClass : 
				pluginsConfiguration.getStringArray("plugins.plugin")) {
			
			try {
				@SuppressWarnings("unchecked")
				Class<Plugin> _class = (Class<Plugin>) Class.forName(pluginClass);
				listOfPlugins.add(_class);
			} catch (Exception e) {
				logger.error("Could not load plugin " + pluginClass, e);
			}
		}
	}
	
	private List<Plugin> factoryPluginsSet() {
		List<Plugin> plugins = new ArrayList<Plugin>();
		
		for (Class<Plugin> c : listOfPlugins) {
			try {
				Plugin plugin = c.newInstance();
				plugin.start();
				plugins.add(plugin);
			} catch (Exception e) {
				logger.error("Could not instantiate plugin " + c.getName(), e);
			}
		}
		
		return plugins;
	}
}
